#!/bin/python
# coding:utf-8
__author__ = 'admin zhoushuang'
__date__ = '2016/9/29'

from common_functions import *
import sys
import gvars
import modimporter
import configurations

class ophcmdline:
    def __init__(self):
        self.req_dict = {}
        self.print_flag = True
        self.host_list = []
        self.host = ''
        self.group = ''
        self.group_list = []

    def process_cmdline(self, sys_argv):
        self.req_dict, self.host_list, self.group_list = parse_cmdline(sys_argv)

        # self.req_dict = {}
        # if len(sys_argv) <= 3:
        #     print 'Usage: oph [-h host] [-g hostgroup] [module_command] [param=value]....'
        #     exit(1)
        # opts, argvs = getopt.getopt(sys_argv[1:], 'h:g:')
        #
        # if len(argvs) < 1:
        #     print 'Usage: oph [-h host] [-g hostgroup] [module_command] [param=value]....'
        #     exit(1)
        #
        # for name, value in opts:
        #     if name == '-h':
        #         self.host = value
        #     if name == '-g':
        #         self.group = value
        #
        # self.req_dict['module'] = 'mod_' + argvs[0]
        #
        # try:
        #     param_dict = parse_argv(argvs[1:])
        # except Exception:
        #     print "Error, parse argv error. argvs[1:] {}=".format(argvs[1:])
        #     exit(1)
        #
        # self.req_dict.update(param_dict)
        # self.host_list += self.host.split(',')


    def call_module(self):
        hosted = []

        if self.host_list:
            for h in self.host_list:
                if h not in hosted:
                    hosted.append(h)
                    call_module_request_side(self.req_dict, (h, gvars.PORT))
                else:
                    print "WARNING\t %s is already executed!"

        if self.group_list:
            for g in self.group_list:
                if g in configurations.HostGroup:
                    hl = configurations.HostGroup.get(g)
                    for h in hl:
                        if h not in hosted:
                            hosted.append(h)
                            call_module_request_side(self.req_dict, (h, gvars.PORT))
                        else:
                            print "WARNING\t [%s] in [%s] is already executed!" % (h, g)
                else:
                    print "WARNING\t [%s] group is not defined in configuration.py !" % g



        # modulename = self.req_dict['module']
        # module = globals().get(modulename, None)
        # if module is None:
        #     print '#Error , There is no module named {} '.format(modulename)
        #     exit(1)
        #
        # handler = getattr(module, 'gethandler', None)()
        # # print dir(handler)
        #
        # for h in self.host_list:
        #     print '@@@ {}'.format(h)
        #     print handler.handle_request_side(self.req_dict, (h, gvars.PORT))


if __name__ == '__main__':
    oph = ophcmdline()
    oph.process_cmdline(sys.argv)
    oph.call_module()
